package nio.bytebuffer;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class AsCharBuffer {

    public static void main(String[] args) throws UnsupportedEncodingException {

        asCharBuffer();

    }

    /**
     * 通过 asCharBuffer() 得到的 CharBuffer，受 ByteBuffer 的影响，反之亦然
     * @throws UnsupportedEncodingException
     */
    private static void asCharBuffer() throws UnsupportedEncodingException {

        byte[] byteArray = "我是中国人".getBytes("utf-16BE");
        ByteBuffer byteBuffer1 = ByteBuffer.wrap(byteArray);
        CharBuffer charBuffer = byteBuffer1.asCharBuffer();
        byteBuffer1.put( 2, "为".getBytes("utf-16BE")[0]);
        byteBuffer1.put( 3, "为".getBytes("utf-16BE")[1]);

        charBuffer.clear();

        for (int i = 0; i < charBuffer.limit(); i++) {
            System.out.print(charBuffer.get() + " ");
        }

    }

    private static void solvedCharset2() throws UnsupportedEncodingException {

        // 字节 数组 使用 的 编码 为 UTF- 8
        byte[] byteArrayIn1 = "我是中国人".getBytes("utf-8");
        System.out.println(Charset.defaultCharset().name()); // UTF-8

        ByteBuffer bytebuffer = ByteBuffer.wrap(byteArrayIn1);
        System.out.println("bytebuffer=" + bytebuffer.getClass().getName()); // java.nio.HeapByteBuffer
        System.out.println("bytebuffer.position=" + bytebuffer.position() // bytebuffer.position=0
                + " bytebuffer.capacity=" + bytebuffer.capacity() // bytebuffer.capacity=15
                + " bytebuffer.limit=" + bytebuffer.limit()); // bytebuffer.limit=15

        // 将 bytebuffer 中的 内容 转成 UTF- 8 编码 的 CharBuffer
        CharBuffer charBuffer = Charset.forName("utf-8").decode(bytebuffer);

        System.out.println("charBuffer=" + charBuffer.getClass().getName()); // java.nio.HeapCharBuffer
        System.out.println("charBuffer.position=" + charBuffer.position() // charBuffer.position=0
                + " charBuffer.capacity=" + charBuffer.capacity() // charBuffer.capacity=15
                + " charBuffer.limit=" + charBuffer.limit()); // charBuffer.limit=5

        System.out.println(charBuffer.capacity());

        charBuffer.position(0);
        for (int i = 0; i < charBuffer.limit(); i++) {
            System.out.print(charBuffer.get() + " ");
        }

    }

    private static void solvedCharset1() throws UnsupportedEncodingException {

        byte[] byteArrayIn1 = "我是中国人".getBytes("utf-16BE");
        // 运行本代码的 *.java 文件是 UTF-8 编码， 所以运行环境取得的编码默认是 UTF-8
        System.out.println(Charset.defaultCharset().name()); // UTF-8

        ByteBuffer bytebuffer = ByteBuffer.wrap(byteArrayIn1);
        System.out.println("bytebuffer=" + bytebuffer.getClass().getName()); // java.nio.HeapByteBuffer
        System.out.println("bytebuffer.position=" + bytebuffer.position() // bytebuffer.position=0
                + " bytebuffer.capacity=" + bytebuffer. capacity() // bytebuffer.capacity=15
                + " bytebuffer.limit=" + bytebuffer. limit()); // bytebuffer.limit=15

        CharBuffer charBuffer = bytebuffer.asCharBuffer();

        System.out.println("charBuffer=" + charBuffer.getClass().getName()); // java.nio.ByteBufferAsCharBufferB
        System.out.println("charBuffer.position=" + charBuffer.position() // charBuffer.position=0
                + " charBuffer.capacity=" + charBuffer.capacity() // charBuffer.capacity=7
                + " charBuffer.limit=" + charBuffer.limit()); // charBuffer.limit=7

        System.out.println(charBuffer.capacity()); // 5

        charBuffer.position(0);
        for (int i = 0; i < charBuffer.capacity(); i++) {
            System.out.print(charBuffer.get() + " "); // 我 是 中 国 人
        }

    }

    private static void unSolveCharset() {

        byte[] byteArrayIn1 = "我是中国人".getBytes();
        // 运行本代码的 *.java 文件是 UTF-8 编码， 所以运行环境取得的编码默认是 UTF-8
        System.out.println(Charset.defaultCharset().name()); // UTF-8

        ByteBuffer bytebuffer = ByteBuffer.wrap(byteArrayIn1);
        System.out.println("bytebuffer=" + bytebuffer.getClass().getName()); // java.nio.HeapByteBuffer
        System.out.println("bytebuffer.position=" + bytebuffer.position() // bytebuffer.position=0
                + " bytebuffer.capacity=" + bytebuffer. capacity() // bytebuffer.capacity=15
                + " bytebuffer.limit=" + bytebuffer. limit()); // bytebuffer.limit=15

        CharBuffer charBuffer = bytebuffer.asCharBuffer();

        System.out.println("charBuffer=" + charBuffer.getClass().getName()); // java.nio.ByteBufferAsCharBufferB
        System.out.println("charBuffer.position=" + charBuffer.position() // charBuffer.position=0
                + " charBuffer.capacity=" + charBuffer.capacity() // charBuffer.capacity=7
                + " charBuffer.limit=" + charBuffer.limit()); // charBuffer.limit=7

        System.out.println(charBuffer.capacity());

        charBuffer.position(0);
        for (int i = 0; i < charBuffer.capacity(); i++) {
            // get() 方法使用的编码为 UTF-16BE
            // UTF-8 与 UTF-16BE 并不是同一种编码
            // 所以这时出现了乱码
            System.out.print(charBuffer.get() + " ");
        }

    }

}
